package net.oschina.android.adapter;

import java.util.ArrayList;
import net.oschina.android.R;
import net.oschina.android.model.NavDrawItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author HuangWenwei
 *
 * @date 2014年8月29日
 */
public class NavDrawerListAdapter extends BaseAdapter {
	
	private Context context;
	private ArrayList<NavDrawItem> navDrawerItems;

	/**
	 * @param context
	 * @param navDrawerItems
	 */
	public NavDrawerListAdapter(Context context,
			ArrayList<NavDrawItem> navDrawerItems) {
		this.context = context;
		this.navDrawerItems = navDrawerItems;
	}

	@Override
	public int getCount() {
		return navDrawerItems.size();
	}

	@Override
	public Object getItem(int position) {
		return navDrawerItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if(convertView == null){
			LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.drawer_list_item, null);
		}
		
		ImageView imgIcon = (ImageView) convertView.findViewById(R.id.im_icon);
		TextView textTitle = (TextView) convertView.findViewById(R.id.tv_title);
		TextView textCount = (TextView) convertView.findViewById(R.id.tv_counter);
		
		imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
		textTitle.setText(navDrawerItems.get(position).getTitle());
		
		if(navDrawerItems.get(position).isCounterVisible()){
			textCount.setText(navDrawerItems.get(position).getCount());
		}else
			textCount.setVisibility(View.GONE);
		
		return convertView;
	}

}
